/*
 * @Description: 按键模块-svc层
 * @Author: yangsw
 * @Date: 2021-06-11 09:49:42
 * @LastEditTime: 2022-06-17 10:18:35
 * @LastEditors: yangsw
 * @Reference:
 */
#include "svc_key.h"
#include "../../log/svc/svc_log.h"
#include "../../lcd/svc/svc_lcd.h"
#include "../../bat/svc/svc_bat.h"
#include "../../ir/svc/svc_ir_uart.h"
#include "../../flash/svc/svc_flash.h"
#include "../../cloud/svc/svc_cloud.h"
#include "../../pulse/svc/svc_pulse.h"

static S_Key *ps_key;
static S_Flash_Data *ps_flash_key;
// static S_Lcd *ps_lcd_key;
static S_Bat *s_bat_key;
static S_Pulse *s_pulse_key;

/**
 * @brief: 按键模块初始化
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Key_Init(S_Key *ps_key_temp)
{
  ps_key = ps_key_temp;
  Bsp_Key_Gpio_Init();
  ps_flash_key = Svc_Flash_Get_Data();
  //  ps_lcd_key = Svc_Lcd_Get_Data();
  s_bat_key = Svc_Bat_Get_Data();
  s_pulse_key = Svc_Pulse_Get_Data();
}

/**
 * @brief: 按键释放后操作
 * @return {void}
 * @note:
 * @see:
 * @param {S_Key} *ps_key 按键相关参数
 */
void Svc_Key_Release(S_Key *ps_key)
{
  if (ps_key->key_cnt >= KEY_6S) // 按键按下的时间超过6s
  {
    ps_key->e_keyPressType = E_KEY_6S_STATE;
  }

  else if ((ps_key->key_cnt >= KEY_3S) && (ps_key->key_cnt < KEY_6S))
  {
    ps_key->e_keyPressType = E_KEY_3S_STATE;
  }
  else if ((ps_key->key_cnt >= 1) && (ps_key->key_cnt < KEY_3S))
  {
    ps_key->e_keyPressType = E_KEY_CLAR_STATE;
  }
  else if ((ps_key->key_set_cnt >= KEY_RELEASE) && ps_key->key_cnt <= 1)
  {
    ps_key->e_keyPressType = E_KEY_1S_STATE;
  }
  else
  {
  }
  ps_key->key_set_cnt = 0;
  ps_key->key_cnt = 0; // 清除按键按下时间计数
}

void Svc_Key_Clear_Key_UpCnt(void)
{
  ps_key->key_up_cnt = 0;
}

/**
 * @brief: 按键按下后操作 ,按下高电平
 * @return {void}
 * @note:
 * @see:
 * @param {S_Key} *ps_key 按键相关参数
 */
void Svc_Key_Press(S_Key *ps_key)
{
  ps_key->key_cnt++;                // 按键按下时间累加
                                    //    Svc_Log_Printf(LOG_INFO, "%s->Key Press:%u s.", __FUNCTION__, ps_key->key_cnt);
  if (ps_key->key_cnt >= KEY_BREAK) // 按键超时
  {
    ps_key->e_keyPressType = E_KEY_BREAK;
    ps_key->key_cnt = 0;
    
  }
  else if (ps_key->key_cnt == KEY_6S) // 显示UP
  {
    Svc_Lcd_Wakeup_Config();
    Svc_Lcd_Display_EnUpdata();
  }
  else if (ps_key->key_cnt == KEY_3S) // 显示IR-OPEN
  {
    Svc_Lcd_Wakeup_Config();
    Svc_Lcd_Display_EnIr();
  }
  else if ((ps_key->key_cnt >= 1) && (ps_key->key_cnt < KEY_3S))
  {    
    Svc_Lcd_Clear_Alarm_Sign();
  }
}

/**
 * @brief: 获取按键当前状态
 * @param  {void}
 * @return {E_Gpio_State} 按键当前状态
 * @note:
 * @see:
 */
E_Gpio_State Svc_Key_Get_State(void)
{
  return Bsp_Key_Get_State();
}

/**
 * @brief: 按键模块是否忙
 * @param  {void}
 * @return {E_Result_Check} E_CHECK_OK:按键模块忙，E_CHECK_FAIL:按键模块空闲
 * @note:
 * @see:
 */
E_Result_Check Svc_Key_Is_Busy(void)
{
  if (ps_key->e_keyPressType != E_KEY_INIT_STATE)
  {
    return E_CHECK_OK;
  }
  return E_CHECK_FAIL;
}

/**
 * @brief: 按键清除lcd报警
 * @param  {void}
 * @return {void}
 * @note:
 * @see:
 */
void Svc_Key_Clear_Lcd_Alarm(void)
{
  gw_u8 u8LcdIndex = Svc_Lcd_Get_Index();

  switch (u8LcdIndex)
  {
  case E_Lcd_Display_Err_0: // 错误代码 - 电量低
  {
    if (s_bat_key->e_power_state != E_Power_Low_1)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Err_Power_Low);
      Svc_Flash_Clear_Device_Alarm(Alarm_Power1);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Power_Low");
    }
    break;
  }
  case E_Lcd_Display_Err_1: // 错误代码 - 电量不足
  {
    if (s_bat_key->e_power_state != E_Power_Low_2)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Err_Power_Solow);
      Svc_Flash_Clear_Device_Alarm(Alarm_Power2);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Power_Solow");
    }
    break;
  }
  case E_Lcd_Display_Err_2: // 错误代码 - 反流
  {
    if (!s_pulse_key->s_err_flow.u8NolFlow)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Reveres_Flow);
      Svc_Flash_Clear_Device_Alarm(Alarm_Reverse_Flow);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Reveres_Flow");
    }
    break;
  }
  case E_Lcd_Display_Err_3: // 错误代码 - 存储异常
  {
    gw_u8 err = 0;
    if (Svc_Flash_Write_Device_Para(&ps_flash_key->s_para) == E_STATE_ERROR)
      err++;
    if (Svc_Flash_Write_Device_Security(&ps_flash_key->s_security) == E_STATE_ERROR)
      err++;
    if (err == 0)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Save_Abn);
      Svc_Flash_Clear_Device_Alarm(Alarm_Save_Abn);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Save_Abn");
    }
  }
  case E_Lcd_Display_Err_4: // 错误代码 - 大流
  {
    if (!s_pulse_key->s_err_flow.u8NolFlow)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Large_Flow);
      Svc_Flash_Clear_Device_Alarm(Alarm_Large_Flow);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Large_Flow");
    }
    break;
  }
  case E_Lcd_Display_Err_5: // 错误代码 - 小流
  {
    if (!s_pulse_key->s_err_flow.u8NolFlow)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Small_Flow);
      Svc_Flash_Clear_Device_Alarm(Alarm_Small_Flow);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Small_Flow");
    }
    break;
  }
  case E_Lcd_Display_Err_6: // 错误代码 - 高温
  {
    if (!s_pulse_key->s_err_flow.u8NolTemp)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Temp_Hight);
      Svc_Flash_Clear_Device_Alarm(Alarm_Temp_Hig);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Temp_Hight");
    }
    break;
  }
  case E_Lcd_Display_Err_7: // 错误代码 - 低温
  {
    if (!s_pulse_key->s_err_flow.u8NolTemp)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Temp_Low);
      Svc_Flash_Clear_Device_Alarm(Alarm_Temp_Low);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Temp_Low");
    }
    break;
  }
  case E_Lcd_Display_Err_8: // 错误代码 - 掉电
  {
    if (s_bat_key->e_power_state != E_Power_Down)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Power_Down);
      Svc_Flash_Clear_Device_Alarm(Alarm_Power_Down);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Power_Down");
    }
    break;
  }
  case E_Lcd_Display_Err_9: // 错误代码 - 空管
  {
    if ((s_pulse_key->u8PulseAlarm & ALARM_BLANK_PIPE) == 0)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Blank_Pipe);
      Svc_Flash_Clear_Device_Alarm(Alarm_Blank_Pipe);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Blank_Pipe");
    }
    break;
  }
  case E_Lcd_Display_Err_10: // 错误代码 - 时间芯片异常
  {
    if ((s_pulse_key->u8PulseAlarm & ALARM_SPI_ABNORMAL) == 0)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Time_Chip);
      Svc_Flash_Clear_Device_Alarm(Alarm_Time_Chip);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Time_Chip");
    }
    break;
  }
  case E_Lcd_Display_Err_11: // 错误代码 - 温度传感器异常
  {
    if ((s_pulse_key->u8PulseAlarm & ALARM_TEMP_SENSOR) == 0)
    {
      Svc_Lcd_Display_Clear_Err(E_Lcd_Temp_Sensor);
      Svc_Flash_Clear_Device_Alarm(Alarm_Temp_Sensor);
      Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
      Svc_Log_Printf(LOG_INFO, "Clear Temp_Sensor");
    }
    break;
  }
  case E_Lcd_Display_Err_12: // 错误代码 - 按键异常
  {
    Svc_Lcd_Display_Clear_Err(E_Lcd_Key_Abn);
    Svc_Flash_Clear_Device_Alarm(Alarm_Key_Abn);
    Svc_Flash_Write_Device_Para(&ps_flash_key->s_para);
    Svc_Log_Printf(LOG_INFO, "Clear Key_Abn");
  }
  default:
    break;
  }
}